using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace FanDongping.Stock
{
    public class Trade
    {
        Stock stock;
        int count = 1000;
        double price, tradeFee, otherFee, stampTax, retainedMoney;
        TradeType tradeType;
        DateTime time;
        bool modified = true;

        [XmlIgnore]
        public bool Modified
        {
            get { return modified; }
            set { modified = value; }
        }

        public double RetainedMoney
        {
            get { return retainedMoney; }
            set
            {
                if (retainedMoney != value)
                {
                    modified = true;
                    retainedMoney = value;
                }
            }
        }

        public Stock Stock
        {
            get { return stock; }
            set
            {
                if (stock != value)
                {
                    modified = true;
                    stock = value;
                }
            }
        }

        public TradeType TradeType
        {
            get { return tradeType; }
            set
            {
                if (tradeType != value)
                {
                    modified = true;
                    tradeType = value;
                }
            }
        }

        public int Count
        {
            get { return count; }
            set
            {
                if (count != value)
                {
                    modified = true;
                    count = value;
                }
            }
        }

        public double TradeFee
        {
            get { return tradeFee; }
            set
            {
                if (tradeFee != value)
                {
                    modified = true;
                    tradeFee = value;
                }
            }
        }

        public double OtherFee
        {
            get { return otherFee; }
            set
            {
                if (otherFee != value)
                {
                    modified = true;
                    otherFee = value;
                }
            }
        }

        public double StampTax
        {
            get { return stampTax; }
            set
            {
                if (stampTax != value)
                {
                    modified = true;
                    stampTax = value;
                }
            }
        }

        public double Price
        {
            get { return price; }
            set
            {
                if (price != value)
                {
                    modified = true;
                    price = value;
                }
            }
        }

        public DateTime Time
        {
            get { return time; }
            set
            {
                if (time != value)
                {
                    modified = true;
                    time = value;
                }
            }
        }

        public double CalcTurnover
        {
            get
            {
                if (tradeType == TradeType.SELL)
                {
                    return (this.Count * this.Price);
                }
                else
                {
                    return -(this.Count * this.Price);
                }
            }
        }

        public double CalcStampTax
        {
            get
            {
                return Math.Abs(CalcTurnover * (double)(StockData.TradeConfig.StampTaxRatio));
            }
        }

        public double CalcTradeFee
        {
            get
            {
                if (tradeType == TradeType.BUY)
                {
                    return Math.Abs(CalcTurnover * (double)StockData.TradeConfig.BuyTradeRatio);
                }
                else
                {
                    return Math.Abs(CalcTurnover * (double)StockData.TradeConfig.SellTradeRatio);
                }
            }
        }

        public decimal CalcOtherFee
        {
            get
            {
                return StockData.TradeConfig.OtherFee;
            }
        }

        public double CalcRetainedMoney
        {
            get
            {
                return CalcTurnover - this.tradeFee - this.stampTax - this.otherFee;
            }
        }
    }

    public enum TradeType
    {
        BUY, SELL
    }
}
